#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2012/06/16

@author: António Baião
'''
from random import randint
from K_d_tree import k_d_tree, No

#lista = [[randint(1, 20),randint(1, 20)] for k in range(10)]
lista = [[2,3], [5,4], [9,6], [4,7], [8,1], [7,2]]
#lista = [[1], [5], [9], [4],[ 8], [7]]

#print len(lista[0])

tree = k_d_tree(12)

tree.count = len(lista)

tree.kdtree(lista)

#tree.print_list()



print "INORDER"
lista = []
tree.inorder_walk(tree.root, lista)

for x in lista:
    print x

noTeste = No()
noTeste.key = [9,12]

x = tree.malloc(noTeste)

tree.insert(x)

print "INORDER"
lista = []
tree.inorder_walk(tree.root, lista)

for x in lista:
    print x
#### Inserir    
noTeste = No()
noTeste.key = [4,3]

x = tree.malloc(noTeste)

tree.insert(x)

print "INORDER"
lista = []
tree.inorder_walk(tree.root, lista)

for x in lista:
    print x    


print "SUCESSOR"
print tree.sucessor(9)

print "PESQUISA"
print tree.search(tree.root, [9,12])

print "DELETE"
tree.delete(5)
tree.free(5)


print "INORDER"
lista = []
tree.inorder_walk(tree.root, lista)

for x in lista:
    print x    
    
#### Inserir    
noTeste = No()
noTeste.key = [10,3]

x = tree.malloc(noTeste)

tree.insert(x)

print "INORDER"
lista = []
tree.inorder_walk(tree.root, lista)

for x in lista:
    print x
    
print "NearestNeighbour"
print tree.searchNearestNeighbour(10)    

#### Inserir    
noTeste = No()
noTeste.key = [12,3]

x = tree.malloc(noTeste)

tree.insert(x)

#### Inserir    
noTeste = No()
noTeste.key = [32,3]

x = tree.malloc(noTeste)

tree.insert(x)

print "Rebalancear"
tree.reBalanceTree()

print "INORDER"
lista = []
tree.inorder_walk(tree.root, lista)

for x in lista:
    print x

